package com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.pmlocation;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.prestamomo.vil.ppa.PmvilApp;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class PmvilOpenLocationMd {
    private static LocationManager locatinPmvilMage = null;
    public static double locationPmvilLat = 0,locationPmvilLot = 0;
    public static Address locationPmvilAddress = null;

    @SuppressLint("MissingPermission")
    public static void LocationOpen() {
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(PmvilApp.getPmvilApss(),
                Manifest.permission.ACCESS_COARSE_LOCATION)) {
         return;
        }
        try {
            if (locatinPmvilMage == null) {
                locatinPmvilMage = (LocationManager) PmvilApp.getPmvilApss().getSystemService(Context.LOCATION_SERVICE);
            }
            String pmvilLocStr =  getPmvilLocString();
            if (pmvilLocStr != null) {
                locatinPmvilMage.requestLocationUpdates(pmvilLocStr, 0, 0f, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        pmvilExtraLocation(location);
                    }
                    private void pmvilExtraLocation(Location pmvilLocation) {
                        locationPmvilLat = pmvilLocation.getLatitude();
                        locationPmvilLot = pmvilLocation.getLongitude();
                        Geocoder gprPmvilGeocoder = new Geocoder(PmvilApp.getPmvilApss(), Locale.getDefault());
                        try {
                            List<Address> locPmvilLprList = gprPmvilGeocoder.getFromLocation(pmvilLocation.getLatitude(), pmvilLocation.getLongitude(), 1);
                            if (locPmvilLprList != null) {
                                locationPmvilAddress = locPmvilLprList.get(0);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (locatinPmvilMage != null) {
                            locatinPmvilMage.removeUpdates(this);
                        }
                        locatinPmvilMage = null;
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                    }
                });
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Nullable
    private static String getPmvilLocString() {
        String pmvilLocStr;
        List<String> pmvilProvider = locatinPmvilMage.getProviders(true);
        if (pmvilProvider.contains(LocationManager.NETWORK_PROVIDER)) {
            pmvilLocStr = LocationManager.NETWORK_PROVIDER;
        } else {
            if (pmvilProvider.contains(LocationManager.GPS_PROVIDER)) {
                pmvilLocStr = LocationManager.GPS_PROVIDER;
            }else {
                pmvilLocStr = null;
            }
        }
        return pmvilLocStr;
    }


}
